import { Component, OnInit } from '@angular/core';
import { RemindService } from "./remind.service";
import { InvestService } from "../../invest.service";
import * as _ from "lodash";
import { Util } from 'app/common/util';

@Component({
  selector: 'app-remind',
  templateUrl: './remind.component.html',
  styleUrls: ['./remind.component.scss'],
  providers: [RemindService]
})
export class RemindComponent implements OnInit {
  // 用户列表全选按钮
  public userCheckedAll: any;
  // 群组列表全选按钮
  public groupCheckedAll: any;
  // 用户列表
  public userList: any = [];
  // 群组列表
  public groupList: any = [];
  // 查询用户列表得到的数据（用户名）
  public finalData: any;
  // 获取用户列表getBody
  public getUserBody: any = {
    pageIndex: 0,
    pageSize: 0,
    sRealname: '',
    sDeptname: '',
    sRolename: ''
  };
  // 获取群组信息getBody
  public getGroupBody: any = {
    pageIndex: 0,
    pageSize: 0,
  };
  // 角色下拉框
  public roleList: any = [];

  constructor(
    public remindService: RemindService,
    public investService: InvestService
  ) { }

  ngOnInit() {
    // 预加载
    Util.loadSelect(this.roleList, 'roleSelect');
    this.getRoleList();
    this.userlist();
    this.grouplist();
  }
  //列表全选
  checkedAll(data, checkAllBox) {
    if (checkAllBox) {
      _.forEach(data, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(data, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll(data, checkAllBtn) {
    let that = this;
    let checkList = [];
    if (data != null) {
      _.forEach(data, item => {
        if (item.isChecked == true) {
          checkList.push(item);
        }
      });
      if (checkList.length == data.length && checkList.length != 0) {
        that.judgeFileType(checkAllBtn, true);
      } else {
        that.judgeFileType(checkAllBtn, false);
      }
    }
  }
  // 判断全选按钮类型
  judgeFileType(checkAllBtn, isTrue) {
    switch (checkAllBtn) {
      // 用户列表
      case 'userCheckedAll':
        this.userCheckedAll = isTrue;
        break;
      //群组列表
      case 'groupCheckedAll':
        this.groupCheckedAll = isTrue;
        break;

    }
  }
  //用户列表
  userlist() {
    let that = this;
    this.getUserBody.sRealname = _.trim(this.getUserBody.sRealname);
    this.getUserBody.sDeptname = _.trim(this.getUserBody.sDeptname);
    this.remindService.getUserList(this.getUserBody).subscribe(data => {
      if (data) {
        that.userList = data.list;
      }
    })
  }
  //群组信息列表
  grouplist() {
    let that = this;
    this.remindService.getGroupList(this.getGroupBody).subscribe(data => {
      if (data) {
        that.groupList = data.list;
      }
    })
  }
  // 发射数据
  saveData() {
    let that = this;
    let emitBody: any = {
      displayName: [],
      userChecked: []
    }
    let groupChecked = [];
    let memberDataList = [];
    _.forEach(this.userList, (item) => {
      if (item.isChecked) {
        emitBody.userChecked.push(item.sUsername);
        emitBody.displayName.push(item.sRealname);
      }
    })
    _.forEach(this.groupList, (item) => {
      if (item.isChecked) {
        groupChecked.push(item.nGroupId)
      }
    })
    if (groupChecked.length > 0) {
      this.remindService.getGroupMember(groupChecked).subscribe((data) => {
        if (data) {
          memberDataList = data;
          // 提取名字进数组
          _.forEach(memberDataList, (item) => {
            emitBody.displayName.push(item.sMbrName);
            emitBody.userChecked.push(item.sMbrName);
          })
          // 去重
          emitBody.displayName = _.uniq(emitBody.displayName).join(",");
          emitBody.userChecked = _.uniq(emitBody.userChecked).join(",");
          that.investService.remindData.emit(emitBody);
          window['$']('#submitRemind').modal('hide');
          window['$']('#editRemind').modal('hide');
          window['$']('#reviewRemind').modal('hide');
          window['$']('#modifyTargetRemind').modal('hide');
        }
      })
    } else {
      emitBody.displayName = _.uniq(emitBody.displayName).join(",");
      emitBody.userChecked = _.uniq(emitBody.userChecked).join(",");
      that.investService.remindData.emit(emitBody);
      window['$']('#submitRemind').modal('hide');
      window['$']('#editRemind').modal('hide');
      window['$']('#reviewRemind').modal('hide');
      window['$']('#modifyTargetRemind').modal('hide');
    }
  }
  // 关闭提醒者模态框
  closeRemind() {
    window['$']('#submitRemind').modal('hide');
    window['$']('#editRemind').modal('hide');
    window['$']('#reviewRemind').modal('hide');
    window['$']('#modifyTargetRemind').modal('hide');
  }
  // 获取角色下拉框
  getRoleList() {
    let that = this;
    this.roleList = [];
    let getBody: any = {
      pageIndex: 0,
      pageSize: 0,
    };
    this.remindService.getRoleList(getBody).subscribe(data => {
      if (data) {
        _.forEach(data.list, item => {
          that.roleList.push({
            sItemKey: item.sRolename,
            sItemValue: item.sRolename
          })
        })
      }
      setTimeout(function () {
        window['$']('#roleSelect').selectpicker('destroy');
        Util.loadSelect(that.roleList, 'roleSelect');
      }, 1);
    })
  }
}
